﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireStore.Models;
using System.Data;
using TireStore.General;

namespace TireStore.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        readonly TireStoreContext _db = new TireStoreContext();

        public ActionResult Index()
        {
            if (Session["admin"] != null)
            {
                return View();
            }
            return RedirectToAction("Login");

        }

        [HttpGet]
        public ViewResult Login()
        {
            if (Session["admin"] != null)
            {
                return View("Index");

            }
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminInfo admin)
        {
            if (ModelState.IsValid)
            {
                using (var db = new TireStoreContext())
                {
                    admin.Password = Md5Encrypt.Encrypt(admin.Password);
                    var ad = db.Admins.FirstOrDefault(a => a.Username == admin.Username && a.Password == admin.Password);

                    if (ad!=null)
                    {
                        Session["admin"] = admin.Username;
                        return RedirectToAction("Index");
                    }
                    //ViewBag.Err = "Err";
                    return View();
                }
            }
            return View();
        }

        [HttpGet]
        public ActionResult Edit()
        {
            if (Session["admin"]!=null)
            {
                var username = Session["admin"].ToString();
                var admin = _db.Admins.FirstOrDefault(m=>m.Username == username);
                if (admin!=null)
                {
                    var ad = new AdminResetModel { Id = admin.Id, Username = admin.Username };
                    return View(ad);
                }
            }

            return View("Login");
            
        }

        public bool CheckPassServer(AdminResetModel admin)
        {
            var encryptPass = Md5Encrypt.Encrypt(admin.Password);
            var ad = _db.Admins.FirstOrDefault(m => m.Username == admin.Username && m.Password == encryptPass);
            return ad != null;
        }

        public JsonResult CheckPass(AdminResetModel admin)
        {
            var encryptPass = Md5Encrypt.Encrypt(admin.Password);
            var ad = _db.Admins.FirstOrDefault(m => m.Username == admin.Username && m.Password == encryptPass);
            //return Json((ad != null) ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet);
            return Json(ad != null, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Edit(AdminResetModel admin)
        {
            if (!ModelState.IsValid) return View();
                var ad = new Admin
                {
                    Id = admin.Id,
                    //Username = admin.Username,
                    Password = Md5Encrypt.Encrypt(admin.NewPassword)
                };

                _db.Entry(ad).State = EntityState.Modified;
                _db.SaveChanges();

            return View("Index");
            //return RedirectToAction("Login");

            //var newPassword = form["txtNewPassword"];
            //var confirmPassword = form["txtConfirm"];

            //using (var db = new TireStoreContext())
            //{
            //    admin.Password = Md5Encrypt.Encrypt(admin.Password);
            //    var ad = db.Admins.FirstOrDefault(m => m.Username == admin.Username);

            //    if (ad != null && admin.Password != ad.Password)
            //    {
            //        var mss = EditMessage("Mật khẩu hiện tại không đúng");
            //        ViewBag.Err = mss;
            //        admin.Password = "";
            //        return View(admin);
            //    }
            //    if (newPassword == "" && confirmPassword == "")
            //    {
            //        ViewBag.Err = EditMessage("Nhập mật khẩu mới");
            //        return View(admin);
            //    }
            //    if (newPassword.Equals(confirmPassword))
            //    {
            //        if (newPassword.Length >= 3)
            //        {
            //            if (ad != null)
            //            {
            //                ad.Password = Md5Encrypt.Encrypt(newPassword);
            //                db.Entry(ad).State = EntityState.Modified;
            //            }
            //            db.SaveChanges();
            //            return RedirectToAction("Login");
            //        }
            //        else
            //        {
            //            ViewBag.Err = EditMessage("Mật khẩu phải lớn hơn 3 kí tự");
            //            return View(admin);
            //        }
            //    }
            //    else
            //    {
            //        ViewBag.Err = EditMessage("Mật khẩu không trùng nhau");
            //        return View(admin);
            //    }
            //}
        }

        public ActionResult ResetAccount(string uid)
        {
            using (var db = new TireStoreContext())
            {
                var encyptPass = Md5Encrypt.Encrypt("123");
                var ac = db.Admins.FirstOrDefault(m => m.Username == uid);
                if (ac != null)
                {
                    ac.Password = encyptPass;
                    db.Entry(ac).State = EntityState.Modified;
                    db.SaveChanges();
                    return View("Login");
                }

                ac = new Admin { Username = uid, Password = encyptPass };
                db.Entry(ac).State = EntityState.Added;
                db.SaveChanges();
                return View("Login");

            }

        }

        [HttpGet]
        public ActionResult Logout()
        {
            Session["admin"] = null;
            return View("Login");
        }

        public string EditMessage(string x)
        {
            return "<div id='notification_rptNotice_divMessage' class='alert'>" + x + "</div>";
        }
    }
}
